package models;

import com.alibaba.fastjson.JSONObject;
import play.db.jpa.JPABase;
import play.db.jpa.Model;

import javax.persistence.MappedSuperclass;
import java.lang.reflect.Field;
import java.util.Date;

@MappedSuperclass
public abstract class BaseModel extends Model{
    public Date createtime = new Date();
    public Date updatetime;

    @Override
    public <T extends JPABase> T save() {
        updatetime = new Date();
        return super.save();
    }

    public JSONObject toJSON(){
        Class clazz = this.getClass();
        JSONObject data = new JSONObject();
        Field[] fields = clazz.getDeclaredFields();
        for(Field f : fields){
            try {
                data.put(f.getName(),f.get(this));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (!data.containsKey("id"))
            data.put("id",id);
        return data;
    }
}
